#include "Thread.hpp"

using namespace MyThread;

void Print(const std::string &name)
{
    // std::cout << "thread running..." << std::endl;

    int cnt = 10;
    while (cnt)
    {
        std::cout << name << ": " << "is running, cnt: " << cnt-- << std::endl;
        sleep(1);
    }
}

const int gnum = 5;
int main()
{
    std::vector<Thread> threads;
    // 构建对象
    for (int i = 0; i < gnum; i++)
    {
        const std::string name = "thread-" + std::to_string(i + 1);
        threads.emplace_back(name, Print);
        sleep(1);
    }

    // 统一启动
    for (int i = 0; i < gnum; i++)
    {
        threads[i].Start();
    }
    sleep(10);

    // 统一停止
    for (int i = 0; i < gnum; i++)
    {
        threads[i].Stop();
    }
    sleep(2);
    // 统一回收
    for (int i = 0; i < gnum; i++)
    {
        threads[i].Join();
    }

    return 0;
}

// int main()
// {
//     Thread t("thread-1", Print);
//     t.Start();
//     std::cout << t.Name() << ": " << " status: " << t.Status() << std::endl;
//     sleep(10);
//     std::cout << t.Name() << ": " << " status: " << t.Status() << std::endl;

//     t.Stop();
//     std::cout << t.Name() << ": " <<" status: " <<  t.Status() << std::endl;
//     sleep(1);

//     t.Join();

//     return 0;
// }